package com.moyu.shopapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.moyu.shopapp.entity.Cart;
import com.moyu.shopapp.entity.Goods;
import com.moyu.shopapp.mapper.GoodsMapper;
import com.moyu.shopapp.service.GoodsService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ZJF
 * @description 针对表【goods】的数据库操作Service实现
 * @createDate 2023-12-01 16:32:18
 */

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    /**
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page<Goods> getAll(int pageNum, int pageSize) {
        Page<Goods> page = new Page<>(pageNum,pageSize);
        return goodsMapper.selectPage(page,null);
    }

    /**
     * 输入关键词进行模糊查找
     * @param searchKey
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page<Goods> getByKey(String searchKey,int pageNum,int pageSize) {
        Page<Goods> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Goods> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(searchKey),Goods::getGoodsName, searchKey)
            .or().like(StringUtils.isNotBlank(searchKey),Goods::getCategory,searchKey)
            .or().like(StringUtils.isNotBlank(searchKey),Goods::getDescription, searchKey);
        return goodsMapper.selectPage(page,lambdaQueryWrapper);
    }

    /**
     * 通过关键词搜索得到的总数
     * @param searchKey
     * @return
     */
    @Override
    public long getByKeyCount(String searchKey) {
        LambdaQueryWrapper<Goods> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(searchKey),Goods::getGoodsName, searchKey)
                .or().like(StringUtils.isNotBlank(searchKey),Goods::getCategory,searchKey)
                .or().like(StringUtils.isNotBlank(searchKey),Goods::getDescription, searchKey);
        return goodsMapper.selectCount(lambdaQueryWrapper);
    }

    /**
     * 获取商品种类
     * @return
     */
    @Override
    @SneakyThrows
    public List<String> getCategoryList() {
        return goodsMapper.selectDistinctCategory();
    }

    /**
     * 通过种类获取列表
     * @param category
     * @return
     */
    @Override
    public List<Goods> getByCategory(String category) {
        LambdaQueryWrapper<Goods> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Goods::getCategory,category);
        return goodsMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 添加购物车后修改库存与销量
     * @param goodsId
     * @param quantity
     * @return
     */
    @Override
    public String updateStockAndSales(int goodsId,int quantity,boolean status){
        Goods goods = goodsMapper.selectById(goodsId);
        int stock = goods.getStock();
        int sales = goods.getSales();
        LambdaUpdateWrapper<Goods> updateWrapper = new LambdaUpdateWrapper<>();
        // 判断是添加订单还是取消订单
        // 取消订单加库存
        // 添加订单减库存
        if (status){
            updateWrapper.set(Goods::getStock,stock-quantity)
                    .set(Goods::getSales,sales+quantity)
                    .eq(Goods::getGoodsId,goodsId);
        }else {
            updateWrapper.set(Goods::getStock,stock+quantity)
                    .set(Goods::getSales,sales-quantity)
                    .eq(Goods::getGoodsId,goodsId);
        }
        goodsMapper.update(null,updateWrapper);
        return "修改成功";
    }

    /**
     * 判断是否还有库存
     * @param goodsId
     * @param quantity
     * @return
     */
    @Override
    public boolean checkStock(int goodsId,int quantity) {
        Goods goods = goodsMapper.selectById(goodsId);
        return goods.getStock()-quantity>0;
    }


}
